package yf.mall.service.impl;

import com.yf.annotation.LoginUserId;
import com.yf.response.RRException;
import com.yf.utils.DateUtil;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.common.Const;
import yf.mall.dao.MallOrderDao;
import yf.mall.entity.MallOrderEntity;
import yf.mall.enums.MallOrderOrderStatusEnum;
import yf.mall.enums.MallOrderPayStatusEnum;
import yf.mall.service.AccountService;
import yf.mall.service.CronService;
import yf.mall.service.MallOrderService;
import yf.mall.vo.MallOrderVo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Log
public class CronServiceImpl implements CronService {

    @Autowired
    private AccountService accountService;
    @Autowired
    private MallOrderDao orderDao;

    //TODO 执行自动确认超过七天没有收货的
    @Scheduled(fixedDelay = 1000 * 60 * 60 * 24)
    @Transactional
    @Override
    public void autoTakeOver() {
        List<MallOrderVo> mallOrderVos = this.orderDao.queryByUserIdAndStatus(null, MallOrderOrderStatusEnum.DELIVERED.getIndex());
        if (mallOrderVos.size() > 0) {

            mallOrderVos.forEach(mallOrderVo -> {

                if (mallOrderVo.getMallOrder().getOrderStatus() == MallOrderOrderStatusEnum.RECEIVED.getIndex()) {
                    return;
                }

                if (DateUtil.getDays(mallOrderVo.getMallOrder().getOrderTime(), new Date()) >= Const.BACK_DAY) {
                    try {
                        MallOrderEntity mallOrder = mallOrderVo.getMallOrder();
                        mallOrder.setOrderStatus(MallOrderOrderStatusEnum.RECEIVED.getIndex());
                        this.orderDao.update(mallOrder);
                        this.accountService.doneTransfer(mallOrder.getOrderNo());
                    } catch (Exception e) {
                        log.warning("自动确认收货失败!订单编号:" + mallOrderVo.getMallOrder().getOrderNo());
                    }

                }
            });

        }
    }

    @Autowired
    private MallOrderService mallOrderService;

    //TODO 执行自动取消一小时没有支付的订单
    @Scheduled(fixedDelay = 1000 * 60 * 60)
    @Transactional
    @Override
    public void autoCancelOrder() {
        List<MallOrderVo> mallOrderVos = this.orderDao.queryByUserIdAndPayStatus(null, MallOrderOrderStatusEnum.NEW.getIndex(), MallOrderPayStatusEnum.NOT_PAID.getIndex());
        mallOrderVos.forEach(mallOrderVo -> {
            try {
                if (DateUtil.getHours(mallOrderVo.getMallOrder().getOrderTime(), new Date()) >= Const.HOUR) {
                    this.mallOrderService.deleteOrder(mallOrderVo.getMallOrder().getUserId(),mallOrderVo.getMallOrder().getId());
                }

            } catch (Exception e) {
                log.warning("取消订单失败!单号" + mallOrderVo.getMallOrder().getOrderNo());
            }
        });
    }


}
